package com.itheima.controller;

import com.google.gson.Gson;
import com.itheima.po.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/les/service")
public class TestController {
    /**
     * 根据车牌号查询订单
     * @param orderQuery
     * @return
     */
    @PostMapping("/S_WL_BC_06")
    public ApiResponse getOrders(@RequestBody OrderQuery orderQuery) {
        // . 构造完整响应
        ApiResponse response = new ApiResponse();
        // 1. 构造系统信息
        SysInfoVo sysInfo = new SysInfoVo();

        String wagonNum =  orderQuery.getWagonNum();


        OrderVo[] orderVos;
        //读取本地json文件
        Gson gson = new Gson();
        try (InputStream inputStream = new ClassPathResource("data/orders.json").getInputStream(); //  直接获取输入流
             Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) { // 指定编码
            orderVos = gson.fromJson(reader, OrderVo[].class);
        } catch (Exception e) {
            throw new RuntimeException("读取JSON失败", e);
        }
        // 2. 构造订单列表
        List<OrderVo> orders = Arrays.stream(orderVos).filter(orderVo -> wagonNum.equals(orderVo.getWagonNum())).toList();

        if (wagonNum.equals("-1") || orders.isEmpty()){

            sysInfo.setMsg("该车辆无订单");
            sysInfo.setTraceId("770000001116809475613880000654");
            sysInfo.setDetailMsg("1");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(-1);
        }
        else{
            sysInfo.setMsg("查询成功");
            sysInfo.setTraceId("770000001116809475613880000653");
            sysInfo.setDetailMsg("1");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(1);
            response.setOrders(orders);
        }


        response.setSysInfo(sysInfo);
        response.setVersion("2.0");
        // _blocks_已初始化为空对象

        return response;

    }

    /**
     * 接受订单信息
     * @param order
     * @return
     */
    @PostMapping("/S_WL_BC_07")
    public ApiResponse getOrders(@RequestBody Order order) {
        ApiResponse response = new ApiResponse();
        SysInfoVo sysInfo = new SysInfoVo();
        if (order.getOrderNum().equals("-1")){
            sysInfo.setMsg("该订单无效");
            sysInfo.setTraceId("770000001116809475613880000655");
            sysInfo.setDetailMsg("");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(-1);
        } else {
            sysInfo.setMsg("成功");
            sysInfo.setTraceId("770000001116809475613880000656");
            sysInfo.setDetailMsg("");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(1);
        }
        response.setSysInfo(sysInfo);
        response.setVersion("2.0");
        // _blocks_已初始化为空对象
        return response;
    }

    /**
     * 喇叭信息
     * @param hornQuery
     * @return
     */
    @PostMapping("/S_WL_BC_15")
    public ApiResponse hornInfo (@RequestBody hornQuery hornQuery) {
        ApiResponse response = new ApiResponse();
        SysInfoVo sysInfo = new SysInfoVo();
        if (hornQuery.getNonce().equals("-1")){
            sysInfo.setMsg("获取喇叭信息失败");
            sysInfo.setTraceId("770000001116809475613880000657");
            sysInfo.setDetailMsg("");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(-1);

            return response;
        } else {
            sysInfo.setMsg("成功");
            sysInfo.setTraceId("770000001116809475613880000658");
            sysInfo.setDetailMsg("");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(1);
        }
        response.setSysInfo(sysInfo);
        response.setVersion("2.0");
        response.setWagonNum("京A123456");
        // _blocks_已初始
        return response;
    }

    @PostMapping("/S_WL_BC_16")
    public ApiResponse getBigScreenInfo(@RequestBody hornQuery hornQuery) {
        ApiResponse response = new ApiResponse();
        SysInfoVo sysInfo = new SysInfoVo();
        if (hornQuery.getNonce().equals("-1")){
            sysInfo.setMsg("获取大屏信息失败");
            sysInfo.setTraceId("770000001116809475613880000659");
            sysInfo.setDetailMsg("");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(-1);
        } else {
            sysInfo.setMsg("查询成功");
            sysInfo.setTraceId("770000001116809475613880000660");
            sysInfo.setDetailMsg("");
            sysInfo.setMsgkey("");
            sysInfo.setStatus(1);
            //排队信息
            List<OrderVo> queuesList = new ArrayList<>();
//            读取本地json文件
            Queue[] queues;
            Gson gson = new Gson();
            try (InputStream inputStream = new ClassPathResource("data/queue.json").getInputStream();
                    Reader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
                queues = gson.fromJson(reader, Queue[].class);  // 映射到Java对象
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            Arrays.stream(queues).forEach(queuesList::add);
            response.setOrders(queuesList);

        }
        response.setSysInfo(sysInfo);
        response.setVersion("2.0");
        // _blocks_已初始化为空对象

        return response;

    }
}
